package com.zzb.learning.service.impl;

import com.zzb.base.model.RestResponse;
import com.zzb.content.model.po.CoursePublish;
import com.zzb.content.model.po.Teachplan;
import com.zzb.learning.feignclient.ContentServiceClient;
import com.zzb.learning.feignclient.MediaServiceClient;
import com.zzb.learning.model.dto.XcCourseTablesDto;
import com.zzb.learning.service.CourseTablesService;
import com.zzb.learning.service.LearningService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author zzb
 * @Date 2024/7/15 14:51
 * @Version 1.0
 * @description
 */
@Service
@Slf4j
public class LearningServiceImpl implements LearningService {

    @Autowired
    private MediaServiceClient mediaServiceClient;
    @Autowired
    private ContentServiceClient contentServiceClient;
    @Autowired
    private CourseTablesService courseTablesService;

    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {
        //查询课程信息
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if (coursepublish == null) {
            throw new RuntimeException("课程信息不存在");
        }
        XcCourseTablesDto courseTablesDto = courseTablesService.getLearnstatus(userId, courseId);
        String learnStatus = courseTablesDto.getLearnStatus();
        if ("702002".equals(learnStatus)) {
            throw new RuntimeException("没有选课或选课后没有支付");
        }
        if ("702003".equals(learnStatus)) {
            throw new RuntimeException("已过期需要申请续期或重新支付");
        }
        if (!"702001".equals(learnStatus)) {
            throw new RuntimeException("其他错误");
        }
        // 判断 该视频是否为试学内容
        Teachplan teachplan = contentServiceClient.getTeachplanById(teachplanId);
        if (!"1".equals(teachplan.getIsPreview())) {
            throw new RuntimeException("该小节暂不支持试学");
        }
        RestResponse<String> result = mediaServiceClient.getPlayUrlByMediaId(mediaId);
        return result;
    }
}
